package main

import "fmt"

/*
 例：寻找最长不含有重复字符的子串
 (1) abcabcbb -> abc
 (2) bbbbb -> b
 (3) pwwkew -> wke

 对于每一个字母x
 (1) lastOccurred[x]不存在，或者< start -> 无需操作
 (2) lastOccurred[x] >= start ->更新start
 (3) 更新lastOccurred[x]，更新maxLength

*/
func main() {
	fmt.Println(lengthOfNonRepeatinqSubStr("abcabcbb"))
	fmt.Println(lengthOfNonRepeatinqSubStr("bbbbb"))
	fmt.Println(lengthOfNonRepeatinqSubStr("pwwkew"))
	fmt.Println(lengthOfNonRepeatinqSubStr(""))
	fmt.Println(lengthOfNonRepeatinqSubStr("b"))
	fmt.Println(lengthOfNonRepeatinqSubStr("abcdef"))
	fmt.Println(lengthOfNonRepeatinqSubStr("这里是慕课网"))
	fmt.Println(lengthOfNonRepeatinqSubStr("一二三二一"))
}

func lengthOfNonRepeatinqSubStr(s string) int {
	lastOccurred := make(map[byte]int)
	start := 0
	maxLength := 0
	for i, ch := range []byte(s) {
		//lastI, ok := lastOccurred[ch]
		//if ok && lastI >= start {
		//	start = lastI + 1
		//}
		if lastI, ok := lastOccurred[ch]; ok && lastI >= start {
			start = lastI + 1
		}
		if i-start+1 > maxLength {
			maxLength = i - start + 1
		}
		lastOccurred[ch] = i
	}

	return maxLength
}
